import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:baseProjectFlutter/base/mvvm/base_mvvm_state.dart';
import 'package:baseProjectFlutter/config/i18n/i18n_key.dart';
import 'package:baseProjectFlutter/router/app_routes_manager.dart';

import '../../base/mvvm/base_state.dart';
import '../../config/i18n/i18n.dart';
import '../../utils/live_data/event_bus.dart';
import '../../utils/other/log/log.dart';
import '../vm/home_vm.dart';

class HomePage extends BasePage {
  const HomePage({super.key});

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends BaseMvvmState<HomePage, HomeVm> {
  _HomePageState() : super(HomeVm());
  final count = 0.obs;

  @override
  void initState() {
    super.initState();

    EventBusKey.TEST.bus(this, (data) {
      LogUtils.e("首页 收到事件: $data");
    });
    title = "首页";
  }

  @override
  Color get backgroundColor => Colors.transparent;

  @override
  PreferredSizeWidget? createAppbar() {
    return null;
  }

  @override
  Widget? floatingActionButton() {
    return FloatingActionButton(
      heroTag: hashCode,
      onPressed: () {
        // if (Get.locale == const Locale('en', 'US')) {
        //   I18nConfig.switchLanguage(const Locale('zh', 'CN'));
        // } else {
        //   I18nConfig.switchLanguage(const Locale('en', 'US'));
        // }
        // VkBleImpl().scan();
        vm.incrementCounter();
      },
      tooltip: 'Increment',
      child: const Icon(Icons.add),
    );
  }

  final List<String> items = List.generate(20, (index) => 'Item $index');

  @override
  Widget buildWidget(BuildContext context) {
    return GridView.builder(
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3, // 每行显示的列数
        crossAxisSpacing: 10.0, // 列间距
        mainAxisSpacing: 10.0, // 行间距
      ),
      itemCount: vm.mainData.length,
      itemBuilder: (context, index) {
        return createItem(vm.mainData[index]);
      },
    );
  }

  Widget createItem(HomeItemData data) {
    return Container(
        alignment: Alignment.center,
        // color: appColors.color1,
        child: SizedBox(
          height: 40,
          child: ElevatedButton(onPressed: () {
            if (data.type == 1) {
              AppRouterJump.login();
            } else if (data.type == 2) {
            }
          }, child: Obx(() {
            return Text("ddd ${vm.count}");
          })),
        ));
  }

  // ElevatedButton(
  // onPressed: () {
  // AppRouterJump.cmdTest();
  // },
  // child: const Text("指令测试")),
  @override
  void dispose() {
    super.dispose();
  }
}
